#!/bin/bash
#THIS VERSION OF SPMOUT MODIFIED PER PW TO SORT COLUMN ORDER.

errors='';
#MAKE SURE MYSQL IS INSTALLED AND THAT THE REQUIRED DATABASE IS PRESENT
#Accept the two digit hour, day, month, and four digit year to operate on.
if [ $# -lt 3 ]; then
	{
		errors="You have fewer than four arguments.";
		errors="$errors You must pass [2 digit day] [2 digit month] [4 digit year] seperated by spaces.";
	}
fi 
#SET UP A VARIABLE DETERMINING THE LENGTH OF THE ERRORS MESSAGE
error_length=`echo $errors | wc -c`;
#create an array of active instruments
actives=`cat Config/config | grep active_inst= | sed 's/"//g' | sed 's/active_inst=/ /g'`
#obtain the saroad number
saroad=`cat Config/config | grep saroad= | sed 's/"//g' | sed 's/saroad=/ /g' | tr -d ' '`
#determine the name of the file to parse
file="$saroad-$3-$1-$2";
#TRANSLATE the active instruments array into an array of data value indicators
calused=''
indics=''
indicator=''
for i in ${actives}
do
	if (stat resources/$i/\.display &> /dev/null); then
		{
			indicator=`(sed 's/\n//g'| sort)<resources/$i/\.display`
			#echo "this indicator is $indicator ";
			if [ "$indics" == "" ]; then
				{
				
					indics="$indicator";
				
				}
			else
				{
				
					indics="$indics $indicator";
				
				}
			fi
		}
	else
		{
	
			if [ "$i" == "environics9100_drv" ]; then
			{
				calused='1'
			}
			fi
			#WHAT SHOULD HAPPEN IF NO DISPLAY FILE IS PRESENT FOR THE CURRENT DRIVER
			sleep 2
		}
	fi

done

if [ $error_length -gt 1 ]; then
	{
		echo $errors;
		exit;
	}
fi


#NOW WE NEED TO INGEST AN HOURS DATA FOR EACH OF THE INDICATORS
#THE INGEST SHOULD BE INDEPENDENT OF SAMPLING FREQUENCY
full_file="Data/$file";
tmpfile=.${$}prcssng 
echo 'use carblogger;' > $tmpfile
#
#select LOG_TIME, Sum( IF( data_label = "O3=", value, 0 )) AS "O3=", Sum( IF( data_label = "RT=", value, 0 )) AS "TEMP", Sum( IF( data_label = "WS=", value, 0 )) AS "WIND SPEED", Sum( IF( data_label = "CO=", value, 0 )) AS "CO" from cl_ingests GROUP BY LOG_TIME;
echo 'select DATE(LOG_TIME) AS "DATE", TIME(LOG_TIME) AS "TIME",' >> $tmpfile
  #CREATE THE CROSS TAB FROM THE INDICATORS BEING USED
countofindics=''
countofindics=`echo "$indics" | wc -w`;
counter='0'
for i in `echo $indics | xargs -n1 | sort -u | xargs`; 
do
	{
		counter=`echo "$counter + 1" | bc`
		columnlabel=`echo $i | sed 's/=//g'`
		if [ "$counter" == "$countofindics" ]; then
		{
			echo "Sum( IF( data_label = \"$i\", value, -0.000 )) AS \"$columnlabel\"">> $tmpfile
		}
		else
		{
			echo "Sum( IF( data_label = \"$i\", value, -0.000 )) AS \"$columnlabel\",">> $tmpfile
		}
		fi
	}
done
echo "FROM cl_ingests WHERE DAY(LOG_TIME) = $1 AND MONTH(LOG_TIME) = $2 AND YEAR(LOG_TIME) = $3 GROUP BY LOG_TIME;">> $tmpfile
PASS=carblogger
#Show how many records needed ingesting
#echo "$counter lines to enter";
#Determine the number of records in the database prior to the operation
#BEFORE=`/usr/bin/mysql -u root --password=$PASS << EOF
#USE carblogger;
#SELECT COUNT(*) FROM cl_ingests;
#EOF
#`

/usr/bin/mysql -u root --password=$PASS --force  < $tmpfile | sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//g'> text_summary_$1$2$3.csv

#rm $tmpfile;
#DEBUG-----
if [ "$4" = "DEBUG" ];then
	{
		echo "indic count is $countofindics";
		cat $tmpfile;
		echo $calused;
		echo $1;
		echo $2;
		echo $3;
		echo $4;
		echo $indics;
		echo $saroad;
		if [ -e "Data/$file" ]; then
		{
		 	echo "$file found";
		}
		else
		{
			echo "$file not found";
		}
		fi
	}
fi
exit 